﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>
}
<style type="text/css">
    .search-condition .condition-item {
        display: -webkit-inline-box;
        line-height: 40px;
    }

        .search-condition .condition-item .mini-combobox, .search-condition .condition-item .mini-textarea {
            width: 300px;
        }

        .search-condition .condition-item .mini-textarea {
            height: 150px;
        }

    .warp {
        width: 75%;
        margin: 0 auto;
    }
</style>
<form id="form1" runat="server" method="post" style="height: 100%">
    <div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
        <table style="width: 100%;">
            <tr>
                <td style="width: 100%;">
                    <span class="separator"></span>
                    <a id="sendMsg" class="mini-button" iconcls="icon-downgrade" onclick="Send" plain="true">发放</a>
                    <span class="separator"></span>
                    <a class="mini-button" iconcls="icon-close" onclick="CloseWindow" plain="true">关闭</a>
                </td>
            </tr>
        </table>
    </div>

    <div class="warp">
        <div class="title">
            <div class="search-condition">
                <div class="condition-item">
                    <label>发送类型：</label>
                    <input id="sendType" name="sendType" class="mini-combobox" onvaluechanged="sendType" value="0" data="sendData" />
                </div>
            </div>
        </div>
        <div class="content-sms">
            <div class="search-condition">
                <div class="condition-item">
                    <label>短信内容：</label>
                    <textarea id="sms_content" class="mini-textarea" emptytext="短信内容"></textarea>
                </div>
                  <table width = "400px" style="margin-left:1px;margin-top:30px">
                    <tr>
                        <td width="100px" height ="50px" align="right" class="fonthsx"><font size ="3">合计：</td>
                        <td width="200px" >
                            <font size ="3" ><span id="FMembers">350条记录</span></font>
                        </td>
                    </tr>
                    <tr>
                        <td width="100px" height ="50px" align="right" class="fonthsx"><font color="#FF0000" size ="3" >提示：</td>
                        <td width="200px" >
                            <font color="#FF0000" size ="3" ><span id="FActionMsg">每70个字符算为一条短信。</span></font>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="content-coupon">
            <div class="search-condition">
                <div class="condition-item">
                    <label style="line-height:52px;">礼券类型：</label>
                    <table>
                        <tr>
                            <td width="110px">
                                <input id="FCouponNumber" name="FCouponNumber" class="mini-buttonedit" style="width: 110px;"
                                    onbuttonclick="getCouponItem('')" onvaluechanged="getCouponValueChanged" selectonfocus="true" />
                            </td>
                            <td width="180px">
                                <input id="FCouponName" name="FCouponName" class="mini-textbox" style="width: 180px; background-color: #CCCCCC;"
                                    enabled="false" align="left" readonly="readonly" />
                            </td>
                        </tr>
                    </table>
                </div>
                <div class="condition-item">
                    <label>礼券面额：</label>
                    <input name="FValue" id="FValue" class="mini-spinner" minValue="0" maxValue="9999999"  showbutton="false" 
                            changeOnMousewheel ="false" style="width:295px;"  value=""  enabled="false"/>
                </div>
                <div class="condition-item">
                    <label>有效期：</label>
                    <input id="beginDate" class="mini-datepicker" enabled="false" />至<input id="endDate" class="mini-datepicker"  enabled="false"/>
                </div>
@*                <div class="condition-item">*@
                    <div id="ckSms" name="ckSms"  class="mini-checkbox" style ="margin-left:100px" readOnly="false" value ="false" text="短信通知用户"></div>
@*                </div>*@
            </div>
        </div>
    </div>
</form>
<input id="FAmount" type="hidden" class="mini-hidden" />
<input id="members" type="hidden" class="mini-hidden" />
<script src="~/Scripts/common.js"></script>
<script type="text/javascript">
    var sendData = [
        { "id": "0", "text": "短信息" },
        { "id": "1", "text": "优惠卷" }
    ];

    function FormShown() {
        $(".content-coupon").hide();
    }

    function SetData(row) {
        var memberData = [];
        for (var i = 0; i < row.row.length; i++) {
            var data = {
                FItemID: row.row[i].FItemID,
                FPhone: row.row[i].FPhone
            }
            memberData.push(data);
        }
        $("#FMembers").html(row.row.length + "个会员.");
        mini.get('members').setValue(JSON.stringify(memberData));
    }

    function Send() {
        mini.get("sendMsg").setEnabled(false);
        var beginDate = mini.get("beginDate").getText();
        var endDate = mini.get("endDate").getText();
        var fCouponId = mini.get('FCouponNumber').getValue();
        var sendType = mini.get("sendType").getValue();
        var smsContent = mini.get('sms_content').getValue();
        var smsChecked = mini.getbyName("ckSms").getChecked() ? 1 : 0;

        if (sendType == "0") {
            if (smsContent == "") {
                mini.showTips({ content: "请输入短信内容!", state: "danger", x: "center", y: "top", timeout: 3000 });
                mini.get("sendMsg").setEnabled(true);
                return;
            }
        } else {
            if (fCouponId == "") {
                mini.showTips({ content: "请选择礼券类型!", state: "danger", x: "center", y: "top", timeout: 3000 });
                mini.get("sendMsg").setEnabled(true);
                return;
            }
            if (beginDate == "") {
                mini.showTips({ content: "请选择礼券开始有效期!", state: "danger", x: "center", y: "top", timeout: 3000 });
                mini.get("sendMsg").setEnabled(true);
                return;
            }
            if (endDate == "") {
                mini.showTips({ content: "请选择礼券结束有效期!", state: "danger", x: "center", y: "top", timeout: 3000 });
                mini.get("sendMsg").setEnabled(true);
                return;
            }
        }

        var param = {
            mSystemAppID: parent.window.getSystemAppID(),
            members: mini.get('members').getValue(),
            FCouponID: fCouponId,
            smsContent: mini.get('sms_content').getValue() + "，拒收请回复R",
            sendType: sendType,
            beginDate: beginDate,
            endDate: endDate,
            FAmount: mini.get("FAmount").getValue(),
            smsChecked: smsChecked,
            FCouponName: '面值' + mini.get("FAmount").getValue() + '元的' + mini.get("FCouponName").getValue()
        }
        postRequest('/Member/SendPreMar', param, function (res) {
            if (res.status == 1) {
                mini.showTips({ content: res.msg, state: "success", x: "center", y: "top", timeout: 3000 });
                //setTimeout(function () {
                //    CloseWindow();
                //}, 2000);

            } else {
                mini.showTips({ content: res.msg, state: "danger", x: "center", y: "top", timeout: 3000 });
                mini.get("sendMsg").setEnabled(true);
            }
        });
    }

    function getCouponData(ID) {
        $.post("../Coupon/GetCoupon", { json: "[{'FItemID':'" + ID + "','mSystemAppID':'" + parent.window.getSystemAppID() + "'}]" }, function (data) {
            var mdata = mini.decode(data);
            var current = new Date();
            mini.get("beginDate").setValue(current);
            if (mdata.data[0].FDateType == 0) {
                current.setFullYear(current.getFullYear() + 10);
                mini.get("endDate").setValue(current);
            } else if (mdata.data[0].FDateType == 1) {
                var dayTime = mdata.data[0].FDays * 24 * 60 * 60 * 1000; //参数天数的时间戳
                var nowTime = current.getTime(); //当天的时间戳
                var t = new Date(nowTime + dayTime).toLocaleString(); //把两个时间戳转换成普通时间
                mini.get("endDate").setValue(t);
            } else if (mdata.data[0].FDateType == 2) {
                mini.get("endDate").setValue(mdata.data[0].FEndDate);
            }
            //mini.get("beginDate").setValue(mdata.data[0].FStartDate);
            //mini.get("endDate").setValue(mdata.data[0].FEndDate);
            mini.get("FAmount").setValue(mdata.data[0].FValue);
            mini.get("FValue").setValue(mdata.data[0].FValue);
        });
    }

    function getCouponItem(mSearchKey) {
        mini.open({
            url: "../GetItem/Index",
            title: "礼券类型选择", width: 730, height: 460,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "new", sTbName: "Coupon", sSearchKey: mSearchKey };
                iframe.contentWindow.SetData(data);//打开GetItemID.js的SetData
            },
            ondestroy: function (action) {
                if (action.length > 0) {
                    mini.getbyName("FCouponNumber").setValue(action[0].FItemID);
                    mini.getbyName("FCouponNumber").setText(action[0].FNumber);
                    mini.getbyName("FCouponName").setValue(action[0].FName);
                    getCouponData(action[0].FItemID);
                }
            }
        });
    }

    function getCouponValueChanged() {
        var sSearchKey = mini.get("FCouponNumber").getText();
        var mAppID = parent.window.getSystemAppID();
        if (sSearchKey == "") return;
        var sTbName = "Coupon";
        var json = "[{";
        json = json + "'FSystemAppID':" + "'" + mAppID + "',";
        json = json + "'FTbName':" + "'" + sTbName + "',";
        json = json + "'FClassID':" + "'0',";
        json = json + "'FNumber':" + "'" + sSearchKey + "'";
        json = json + "}]";
        $.post("../GetItem/GetItemList", { datajson: json }, function (text) {
            var sdata = mini.decode(text);
            if (sdata.data.length == 1) {
                mini.get("FCouponNumber").setValue(sdata.data[0].FItemID);
                mini.get("FCouponNumber").setText(sdata.data[0].FNumber);
                mini.get("FCouponName").setValue(sdata.data[0].FName);
                getCouponData(sdata.data[0].FItemID);
            }
            else {
                mini.get("FCouponNumber").setValue("");
                mini.get("FCouponNumber").setText("");
                mini.get("FCouponName").setValue("");
            }
        });
    }

    function sendType() {
        if (this.getValue() == 0) {
            $(".content-sms").show();
            $(".content-coupon").hide();
        } else {
            $(".content-sms").hide();
            $(".content-coupon").show();
        }
    }
</script>
